/*
 * Copyright (C), 2010-2023, ATCOM Tech. Co., Ltd.
 *
 * File Name: demo.c
 * Author: liaoxuecheng
 * mail: liaoxuecheng@atcom.com.cn
 * Created Time: 2024年10月10日 星期四 09时37分22秒
 *
 */

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

#define THIS_FILE "demo.c"

#ifndef PJ_LOG
#define TEST_PRINTF(title,fmt,arg...) printf("%s:%d %s, "fmt, __func__, __LINE__, title, ##arg)
#define PJ_LOG(arg, fmt) printf("level = %d, ",arg);TEST_PRINTF fmt;
#endif

#include <stdio.h>  
#include <stdlib.h>   // EXIT_FAILURE
#include <dlfcn.h>    // dlopen, dlerror, dlsym, dlclose

#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))

typedef int(* FUNC_ADD)(int, int); // 定义函数指针类型的别名

int main(int argc, char **argv)
{
    int i = 0;
    void *handle = NULL;
    const char *sopath[] = { 
        "output/libtest1.so",
        "output/libtest2.so",
        "output/libtest3.so",
        "output/libtest4.so",
    };

    for(i = 0; i < ARR_SIZE(sopath); i++)
    {
        handle = dlopen(sopath[i], RTLD_NOW);
        if(!handle)
        {
            fprintf( stderr, "%s:%d dlopen %s, get error: %s\n", __FILE__, __LINE__, sopath[i], dlerror());
            continue;
        }
        FUNC_ADD add_func = (FUNC_ADD)dlsym(handle, "my_add");
        if(!add_func)
        {
            fprintf( stderr, "%s:%d dlopen %s, get error: %s\n", __FILE__, __LINE__, sopath[i], dlerror());
            continue;
        }
        int sum = add_func(1,2);
        printf( "1 add 2 is %d\n", sum);
        dlclose(handle);
    }
    return 0;
}

